from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from .models import Coupon,CouponType,CouponTypeValue,GetCoupon

# Create your views here.
class Coupons(APIView):
    # 获取所有优惠券类型
    def get(self,request):
        coupon=CouponType.objects.all()
        type=[]
        for i in coupon:
            type.append({
                'id':i.id,
                'type_name':i.type_name
            })
        return Response(type)

class CouponView(APIView):
    # 获取所有优惠券
    def get(self,request):
        page = int(request.query_params.get('page'))
        pagesize = int(request.query_params.get('pagesize'))
        coupon=Coupon.objects.all()[(page - 1) * pagesize:(page * pagesize)]
        coupon_list=[]
        for i in coupon:
            coupon_list.append({
                'id':i.id,
                'name': i.name,
                'brief': i.brief,
                'coupon_type': i.coupon_type.type_name,
                'goods_name': i.good.name,
                'coupon_num': i.coupon_num,
                'start_time': i.start_time,
                'over_time': i.over_time,
                'start_get_time': i.start_get_time,
                'end_get_time': i.end_get_time,
                'remark': i.remark,
            })
        return Response({
            'code': 200,
            'msg': '获取全部成功',
            'result': coupon_list,
            'total': Coupon.objects.all().count()
        })
    # 添加优惠券
    def post(self,request):
        name=request.data.get('name')
        brief=request.data.get('brief')
        coupon_type=request.data.get('coupon_type')
        good=request.data.get('good')
        coupon_num=request.data.get('coupon_num')
        start_time=request.data.get('start_time')
        over_time=request.data.get('over_time')
        start_get_time=request.data.get('start_get_time')
        end_get_time=request.data.get('end_get_time')
        remark=request.data.get('remark')
        try:
            Coupon.objects.create(
                name=name,
                brief =brief,
                coupon_type_id = coupon_type,
                good_id = good,
                coupon_num = coupon_num,
                start_time = start_time,
                over_time = over_time,
                start_get_time = start_get_time,
                end_get_time = end_get_time,
                remark = remark,
            )
        except Exception as e:
            print(e)
            return Response({'code':400,'msg':'前端参数错误'})
        return Response({'code': 200, 'msg': '添加优惠券成功'})
class CouponInfo(APIView):
    # 获取单个优惠券
    def get(self,request,id):
        try:
            coupon = Coupon.objects.get(id=id)
        except Exception as e:
            print(e)
            return Response({'code':204,'msg':'优惠券获取失败'})
        return Response({
            'name':coupon.name,
            'brief': coupon.brief,
            'coupon_type': coupon.coupon_type_id,
            'good': coupon.good_id,
            'coupon_num': coupon.coupon_num,
            'start_time': coupon.start_time,
            'over_time': coupon.over_time,
            'start_get_time': coupon.start_get_time,
            'end_get_time': coupon.end_get_time,
            'remark': coupon.remark,
        })
    # 修改优惠券
    def put(self,request,id):
        name = request.data.get('name')
        brief = request.data.get('brief')
        coupon_type = request.data.get('coupon_type')
        good = request.data.get('good')
        coupon_num = request.data.get('coupon_num')
        start_time = request.data.get('start_time')
        over_time = request.data.get('over_time')
        start_get_time = request.data.get('start_get_time')
        end_get_time = request.data.get('end_get_time')
        remark = request.data.get('remark')
        try:
            coupon = Coupon.objects.filter(id=id).update(
                name=name,
                brief=brief,
                coupon_type_id=coupon_type,
                good_id=good,
                coupon_num=coupon_num,
                start_time=start_time,
                over_time=over_time,
                start_get_time=start_get_time,
                end_get_time=end_get_time,
                remark=remark,
            )
        except Exception as e:
            print(e)
            return Response({'code':204,'msg':'优惠券修改失败'})
        return Response({'code': 204, 'msg': '优惠券修改成功'})
    # 删除优惠券
    def delete(self,request,id):
        try:
            coupon = Coupon.objects.filter(id=id).delete()
        except Exception as e:
            print(e)
            return Response({'code':204,'msg':'优惠券删除失败'})
        return Response({'code': 204, 'msg': '优惠券删除成功'})